package com.example.pattern.draw.shape

import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.PointF

abstract class Shape {
    var strokeColor:Int = Color.WHITE
    var strokeWidth:Int = 0
    open var startPoint = PointF(0f,0f)
    var endPoint = PointF(0f,0f)
    val mPaint:Paint by lazy{
        Paint().also {
            it.isAntiAlias = true
           it.style = Paint.Style.STROKE
            it.color = strokeColor
            it.strokeWidth = strokeWidth.toFloat()
        }
    }
    private var mTranslateX = 0f

    private var mTranslateY = 0f
    fun move(x:Float,y:Float){
        mTranslateX = x
        mTranslateY = y
    }
    abstract fun containsPoint(x:Float,y:Float):Boolean
    abstract fun drawShape(canvas: Canvas)
    fun fillColor(color:Int){
        mPaint.style = Paint.Style.FILL
        mPaint.color = color
    }
    fun draw(canvas:Canvas){
        //把当前已经绘制好的内容保存起来
        //创建一个新图册
        canvas.save()
        if(mTranslateX != 0f || mTranslateY != 0f){
            canvas.translate(mTranslateX,mTranslateY)
        }
        drawShape(canvas)
        initData()
        //合并图层
        canvas.restore()
    }
    private fun initData(){
        startPoint.x = startPoint.x + mTranslateX
        startPoint.y = startPoint.y + mTranslateY
        endPoint.x = endPoint.x + mTranslateX
        endPoint.y = endPoint.y + mTranslateY
        mTranslateX = 0f
        mTranslateY = 0f
    }
}